import 'package:dio/dio.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:wu_you/data/repository/luck_repository.dart';
import 'package:wu_you/data/repository/purchase_repository.dart';
import 'package:wu_you/data/repository/user_repository.dart';
import 'package:wu_you/route/app_routers.dart';
import 'package:wu_you/utils/error_handler.dart';

///广告
class AdPlugin {
  static const String _CHANNEL_JJ_AD = "jj/ad";

  static const MethodChannel _adChannel = const MethodChannel(_CHANNEL_JJ_AD);

  factory AdPlugin() => _getInstance();
  static AdPlugin _instance;

  AdPlugin._internal();
  String prompt;
  bool _joinTask = false;

  int onlyOne = 0;
  static AdPlugin _getInstance() {
    if (_instance == null) {
      _instance = AdPlugin._internal();
    }
    return _instance;
  }
  void init() {
    _adChannel.setMethodCallHandler(_onMethodCall);
  }
  Future<dynamic> _onMethodCall(MethodCall call) async{
    if("onError" == call.method){
      return _onError(call.arguments);
    }else if("onClose" == call.method){
      return _onClose(call.arguments);
    }else if("onReward" == call.method){
      return _onReward(call.arguments);
    }
    return null;
  }

  String currentType;

  ///抽奖需要
  String currentSn;
  String currentNumber;
  ///打开广告
  Future<bool> showAd({String type,String sn,String number}) async {
    currentType = type;
    currentSn = sn;
    currentNumber = number;
    try {
      Map<String, dynamic> map = {"type":type};
      return _adChannel.invokeMethod("showAd",map);
    } on PlatformException {
      return false;
    }

  }
  void _onClose(String id)async{
    if(Navigator.canPop(AppGlobalKeys.navigatorKey.currentContext)){
      Navigator.of(AppGlobalKeys.navigatorKey.currentContext).pop(prompt);
    };
  }

  ///广告失败也执行
  void _onError(String p) async{
    if(_joinTask == false){
      await _onTaskReward(p);
    }
    _joinTask = false;
  }

  void _onReward(String id) async{
    if(_joinTask == false){
      await _onTaskReward(id);
    }
    _joinTask = false;
  }

  ///任务模块
  Future<dynamic> _onTaskReward(String id) {
    _joinTask = true;
    if(currentType == "10"){    ///销售奖金领取
      UserRepository().getSaleReward().then((value){
        prompt = value.toString();
      }).catchError((error) {
        if(error is String){
          prompt = error;
        }
        if(error is DioError) {
          ErrorHandler.handleError(error, action: (msg) {
            prompt = msg;
          });
        }
      });
    }else if(currentType == "11"){  ///中奖福利领取
      UserRepository().getWinningReward().then((value){
        prompt = value.toString();
      }).catchError((error) {
        if(error is String){
          prompt = error;
        }
        if(error is DioError) {
          ErrorHandler.handleError(error, action: (msg) {
            prompt = msg;
          });
        }
      });
    }else if(currentType == "12"){ ///周分红领取
      UserRepository().getWeekReward().then((value){
        prompt = value.toString();
      }).catchError((error) {
        if(error is String){
          prompt = error;
        }
        if(error is DioError) {
          ErrorHandler.handleError(error, action: (msg) {
            prompt = msg;
          });
        }
      });
    }else if(currentType == "13"){  ///采购返现领取
      PurchaseRepository().purchaseReceive().then((value){
        prompt = value.toString();
      }).catchError((error) {
        if(error is String){
          prompt = error;
        }
        if(error is DioError) {
          ErrorHandler.handleError(error, action: (msg) {
            prompt = msg;
          });
        }
      });
    }else if(currentType == "14"){  ///抽奖确认
      LuckRepository().luckStart(currentSn, currentNumber).then((value){
        prompt = value.toString();
      }).catchError((error){
        if(error is String){
          prompt = error;
        }
        if(error is DioError) {
          ErrorHandler.handleError(error, action: (msg) {
            prompt = msg;
          });
        }
      });
    }
    return null;
  }


  Future<bool> setAdUserId(String inviteCode) async {
    try {
      return _adChannel.invokeMethod("setUserId",inviteCode);
    } on PlatformException {
      return false;
    }
  }
  Future<bool> clearAdUserId() async {
    try {
      return _adChannel.invokeMethod("clearAdUserId");
    } on PlatformException {
      return false;
    }
  }
}
